package com.bdsy.business.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bdsy.api.annotation.IgnoreAuth;
import com.bdsy.business.model.Order;
import com.bdsy.business.model.OrderDetails;
import com.bdsy.business.model.ZlOrder;
import com.bdsy.business.model.ZlOrderDetails;
import com.bdsy.business.service.ShopCarService;
import com.bdsy.business.service.ZlOrderDetailsService;
import com.bdsy.business.service.ZlOrderService;
import com.bdsy.common.annotation.Json;
import com.bdsy.common.utils.DateRandomUtil;
import com.bdsy.common.utils.Result;
import com.bdsy.system.annotation.SysLog;

/**
 * 租赁订单信息
 * @author MING
 * @date 2018年1月30日 下午3:11:52
 */

@Controller
@RequestMapping(value = {"${web.adminPath}", "${web.appPath}"})
public class ZlOrderController {

	@Autowired
	ZlOrderService zlOrderService;
	
	@Autowired
	ZlOrderDetailsService zlOrderDetailsService;
	
	@Autowired
	ShopCarService shopCarService;
	
	/**
	 * 查询当前用户租赁订单信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "zlOrderById")
	public Object zlOrderById(@Param("userId")String userId) {
		Map<String,Object> map = new LinkedHashMap<>();
		@SuppressWarnings("rawtypes")
		List<Map> zlOrderList = zlOrderService.selectZlOrderById(userId);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", zlOrderList);
		return map;
	}
	
	
	/**
	 * 查询租赁订单信息 
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "selectZlOrderList")
	public Object selectZlOrderList(String userId,int status) {
		Map<String,Object> map = new LinkedHashMap<>();
		Map<String,Object> map2 = null;
		Map<String,Object> map3 = new LinkedHashMap<>();
		@SuppressWarnings("rawtypes")
		List<Map> list  = new ArrayList<>();
		map.put("code", 200);
		map.put("msg", "success");
		//获得所有订单ID
		List<String> zlOrderIdList =  zlOrderService.selectZlOrderId(userId,status);
		//根据订单ID 查询订单信息
		for (int i = 0; i < zlOrderIdList.size(); i++) {
			// 测试 不适用 0 有问题 
			@SuppressWarnings("rawtypes")
			List<Map> zlOrderList  = zlOrderService.selectZlOrderByOrderId(zlOrderIdList.get(i),0);
			map2 = new LinkedHashMap<>();
			map2.put("orderId"+i, zlOrderIdList.get(i));
			zlOrderList.add(map2);
			map3.put("orderList"+i, zlOrderList);
		}
		list.add(map3);
		map.put("data", list);
		return map;
	}
	
	
	/**
	 * 根据用户、状态 查询当前用户租赁订单信息(只有订单) 
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "zlOrderListById")
	public Object zlOrderListById(HttpServletRequest request,String userId,int zlStatus,int status) {
		Map<String,Object> map = new LinkedHashMap<>();
		List<Map> zlOrderList = zlOrderService.zlOrderListById(userId,zlStatus,status);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", zlOrderList);
		return map;
	}
	
	/**
	 * 根据租赁订单编号查询租赁订单信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "selectZlOrderByZlId")
	public Object selectZlOrderByZlId(HttpServletRequest request,String zlId,int status) {
		Map<String,Object> map = new LinkedHashMap<>();
		List<Map> zlOrderList = zlOrderService.selectZlOrderByOrderId(zlId,status);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", zlOrderList);
		return map;
	}
	
	
	// 查询租赁订单信息  未发货 未收货 已完成 
	/**
	 * 根据租赁订单编号状态查询订单信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "zlOrderListByIdStatus")
	public Object zlOrderListByIdStatus(String userId,int status) {
		Map<String,Object> map = new LinkedHashMap<>();
		List<Map> zlOrderList = zlOrderService.zlOrderListByIdStatus(userId, status);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", zlOrderList);
		return map;
	}
	
	

	/**
	 * 添加租赁订单信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "addZlOrder")
	public Object addZlOrder(@Param(value="zlOrder")ZlOrder zlOrder,@Param(value="zlOrderDetails")ZlOrderDetails zlOrderDetails) {
		
		//收货地址reId 用户id ?
		String num = DateRandomUtil.getRandomFileName();
		zlOrder.setZlId(num);
		boolean result = zlOrderService.insert(zlOrder);
		//租赁订单详情
		zlOrderDetails.setZlId(num);
		boolean result2 = zlOrderDetailsService.insert(zlOrderDetails);
		if (result && result2) {
			return Result.ok("addSuccess");
		} else {
			return Result.error("addFail");
		}
	}
	
	/**
	 * 修改租赁订单状态 传入status状态值
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "updateZlOrderStatus")
	public Object updateZlOrderStatus(ZlOrder zlOrder) {
		boolean result = zlOrderService.updateZlOrderStatus(zlOrder);
		if (result) {
			return Result.ok("updateSuccess");
		} else {
			return Result.error("updateFail");
		}
	}
	
	/**
	 * 修改租赁订单详情状态
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "updateZlOrderDetailsStatus")
	public Object updateZlOrderDetailsStatus(ZlOrderDetails zlOrderDetails) {
		boolean result = zlOrderDetailsService.updateZlOrderDetailStatus(zlOrderDetails);
		if (result) {
			return Result.ok("updateSuccess");
		} else {
			return Result.error("updateFail");
		}
	}
	
	/**
	 * 购物车生成租赁订单信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "addZlOrderInfo")
	public Object addZlOrderInfo(ZlOrder zlOrder,HttpServletRequest request,String ids) {
		String or = request.getParameter("or");
		List<ZlOrderDetails> list = new ArrayList<ZlOrderDetails>();  
	    list = JSONObject.parseArray(or, ZlOrderDetails.class);  
		//收货地址reId 用户id ?
		String num = DateRandomUtil.getRandomFileName();
		zlOrder.setZlId(num);
		boolean result = zlOrderService.insert(zlOrder);
		//订单详情
		for (ZlOrderDetails zlOrderDetail : list) {
			zlOrderDetail.setZlId(num);
		}
		boolean result2 = zlOrderDetailsService.insertZlOrderDetails(list);
		//清除购物车内的商品
		List<String> lis = JSON.parseArray(ids, String.class);
		boolean result3 = shopCarService.deleteShopCar(lis);
		
		if (result && result2 && result3) {
			return Result.ok("addSuccess");
		} else {
			return Result.error("addFail");
		}
	}
	
	
	/**
	 * 删除当前用户租赁订单信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "deleteZlOrderById")
	public Object deleteZlOrderById(String ids) {
		List<String> list = JSON.parseArray(ids, String.class);
		boolean result = zlOrderService.deleteZlOrderById(list);
		boolean result2 = zlOrderDetailsService.deleteZlOrderDetailsById(list);
		if (result && result2) {
			return Result.ok("deleteSuccess");
		} else {
			return Result.error("deleteFail");
		}
	}
	
	//后台
	@SysLog("当前用户 租赁 商品的状态（付款 发货 收货 完成） ")
	@RequestMapping(value = "zorderListByUserIdStatus")
	@ResponseBody
	public Object zorderListByUserIdStatus(@RequestParam(value="userId",required=false)String userId,@RequestParam(value="shopTypeId",required=false)int shopTypeId,int zlStatus,int status){
		@SuppressWarnings("rawtypes")
		List<Map> zlOrderList = zlOrderService.zorderListByUserIdStatus(userId, shopTypeId, zlStatus, status);
		return zlOrderList;
	}
	
	@SysLog("发货状态")
	@Json
	@RequestMapping(value = "updateZlOrderDetailStatus")
	public Object updateZlOrderDetailStatus(ZlOrderDetails zlOrderDetails) {
		boolean result = zlOrderDetailsService.updateZlOrderDetailStatus(zlOrderDetails);
		if (result) {
    		return Result.ok("发货成功");
		}else{
			return Result.error("发货失败");
		}
	}
}
